﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Globalization;
using System.Security;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using DevExpress.XtraEditors;
using PharmacyPharnet.Core;
using Pharmacy.Data;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Ultilities.Configurations;
using Pharmacy.Ultilities;

namespace PharmacyPharnet
{
    public partial class FrmConfiguration : DialogViewBase
    {
        #region Declaration
        string _strMsg = string.Empty;
        #endregion

        #region Constructors
        /// <summary>
        /// Default Constructor
        /// </summary>
        public FrmConfiguration()
        {
            InitializeComponent();            
        }        
        #endregion Constructors

        #region Private Methods                

        private bool ValidateDBConnection()
        {
            _strMsg = string.Empty;
            bool bFlag = true;
            DevExpress.XtraTab.XtraTabPage activePage = null;
            string strSystemConnection = string.Format(Constant.CONNNECTION_STRING
                                                         , txtOmsServer.Text.Trim()
                                                         , txtOmsDatabase.Text.Trim()
                                                         , txtOmsUsername.Text.Trim()
                                                         , txtOmsPassword.Text.Trim());
            if (ObjectContextFactory.TestConnection(strSystemConnection))
            {
                _strMsg = "1. Connect to the System Database sucessfully.\r\n";
                txtOmsDatabase.ErrorText = string.Empty;
                txtOmsServer.ErrorText = string.Empty;
                txtOmsUsername.ErrorText = string.Empty;
                txtOmsPassword.ErrorText = string.Empty;
                bFlag = true;
            }
            else
            {
                _strMsg = "1. ERROR: Can not connect to the System Database.\r\n";
                txtOmsServer.ErrorText = "Can not connect to the System Database.";
                txtOmsDatabase.ErrorText = txtOmsServer.ErrorText;
                txtOmsUsername.ErrorText = txtOmsServer.ErrorText;
                txtOmsPassword.ErrorText = txtOmsServer.ErrorText;
                bFlag = false;
            }
            if (!bFlag)
            {
                activePage = tabDBConnection;
                XtrTabConfig.SelectedTabPage = activePage;
            }
            return bFlag;
        }
        
        #endregion Private Methods

        #region Public Methods
        #endregion Public Methods

        #region Events

        /// <summary>
        /// Fired when the form is loaded.
        /// </summary>
        /// <author></author>
        /// <history>
        /// 2011.06.23 - ThangTran: added code to save AutoAssignMasterEvent config, task #2653.
        /// </history>
        private void FrmConfiguration_Load(object sender, EventArgs e)
        {
            //Load DBConnection parameters
            txtOmsServer.Text = ConfigManager.DIOConfig[Constant.KEY_SERVER];
            txtOmsDatabase.Text = ConfigManager.DIOConfig[Constant.KEY_DATABASE];
            txtOmsUsername.Text = ConfigManager.DIOConfig[Constant.KEY_USERID];
            txtOmsPassword.Text = CryptHelper.Decrypt(ConfigManager.DIOConfig[Constant.KEY_PASSWORD]);
            
        }

        /// <summary>
        /// Fired when the button Save is clicked.
        /// </summary>
        /// <author></author>
        /// <history>
        /// 2011.06.23 - ThangTran: added code to save AutoAssignMasterEvent config, task #2653.
        /// </history>
        private void cmdSave_Click(object sender, EventArgs e)
        {
            //Validate DB Connection
            if (!ValidateDBConnection())
            {
                return;
            }

            ConfigManager.DIOConfig[Constant.KEY_SERVER] = txtOmsServer.Text.Trim();
            ConfigManager.DIOConfig[Constant.KEY_DATABASE] = txtOmsDatabase.Text.Trim();
            ConfigManager.DIOConfig[Constant.KEY_USERID] = txtOmsUsername.Text.Trim();
            ConfigManager.DIOConfig[Constant.KEY_PASSWORD] = CryptHelper.Encrypt(txtOmsPassword.Text.Trim());            

            ///                
            this.DialogResult = DialogResult.Yes;

            MessageBox.Show("Chương trình cần khởi động lại.");            

            this.Close();
        }

        private void FrmConfiguration_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (MDIController.MDIMainForm == null ||
                MDIController.MDIMainForm.MdiChildren.Length == 0 ||
                MDIController.MDIMainForm.MdiChildren[0].GetType() == this.GetType())
            {
                if (this.DialogResult.Equals(DialogResult.Yes))
                {
                    Application.Restart();
                }
            }
        }

        private void FrmConfiguration_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult.Equals(DialogResult.No))
            {
                e.Cancel = true;
            }
            else if (this.DialogResult.Equals(DialogResult.Yes))
            {

                if (MDIController.MDIMainForm != null)
                {
                    foreach (Form frm in MDIController.MDIMainForm.MdiChildren)
                    {
                        //If FrmConfiguration is shown in MDIMainForm, it will cause endless loop when closing form.
                        if (frm.GetType() != this.GetType())
                        {
                            frm.Close();
                        }
                    }
                }
            }
        }

        #endregion Events        

        
        
    }
}